﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Net;
using System.Net.NetworkInformation; 
namespace Infrastructure.WebMvc.MvcAttributes
{
    [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class ServerValidateAttribute : ActionMethodSelectorAttribute
    {
        public string Target { get; set; }
        public ServerValidateAttribute(string target)
        {
            this.Target = target; 
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
            bool result = false; 
            try
            {
                Ping ping = new Ping();
                var res = ping.Send(Target, 5000);
                switch (res.Status)
                {
                    case IPStatus.BadDestination:
                        break;
                    case IPStatus.BadHeader:
                        break;
                    case IPStatus.BadOption:
                        break;
                    case IPStatus.BadRoute:
                        break;
                    case IPStatus.DestinationHostUnreachable:
                        break;
                    case IPStatus.DestinationNetworkUnreachable:
                        break;
                    case IPStatus.DestinationPortUnreachable:
                        break;
                    case IPStatus.DestinationProhibited:
                        break;
                 
                    case IPStatus.DestinationScopeMismatch:
                        break;
                    case IPStatus.DestinationUnreachable:
                        break;
                    case IPStatus.HardwareError:
                        break;
                    case IPStatus.IcmpError:
                        break;
                    case IPStatus.NoResources:
                        break;
                    case IPStatus.PacketTooBig:
                        break;
                    case IPStatus.ParameterProblem:
                        break;
                    case IPStatus.SourceQuench:
                        break;
                    case IPStatus.Success:
                        result = true; 
                        break;
                    case IPStatus.TimeExceeded:
                        break;
                    case IPStatus.TimedOut:
                        break;
                    case IPStatus.TtlExpired:
                        break;
                    case IPStatus.TtlReassemblyTimeExceeded:
                        break;
                    case IPStatus.Unknown:
                        break;
                    case IPStatus.UnrecognizedNextHeader:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                result =   false; 
            }
            return result; 
        }

    }
}
